<template>
  <el-dialog
    :append-to-body="true"
    @close="closeDialog"
    :visible.sync="dialog"
    :title="is_edit?'编辑':'新增'"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input placeholder="输入名称" v-model="form.name" style="width: 320px;" />
      </el-form-item>
      <el-form-item label="别名" prop="alias">
        <el-input placeholder="输入别名" v-model="form.alias" style="width: 320px;" />
      </el-form-item>
      <el-form-item label="上级类目" prop="pid">
        <treeselect
          v-model="form.pid"
          :options="permissions"
          style="width: 320px;"
          placeholder="选择上级类目"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  _permissionsCreate,
  _permissionsUpdate,
  _permissionsAll
} from "@/http/ajax";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "edit",
  props: {
    is_edit: {
      type: Boolean,
      required: true
    },
    cur_this: {
      type: Object,
      required: null
    }
  },
  data() {
    return {
      permissions: [],
      statusModul: [
        { id: 1, label: "启用", value: true },
        { id: 2, label: "禁用", value: false }
      ],
      dialog: false,
      loading: false,
      form: {
        name: "",
        pid: 0,
        alias: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        alias: [{ required: true, message: "请输入别名", trigger: "blur" }],
        pid: [{ required: true, message: "请选择上级类目", trigger: "blur" }]
      }
    };
  },
  created() {},
  methods: {
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.dialog = true;
          if (this.is_edit) {
            _permissionsUpdate(
              this.form,
              res => {
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          } else {
            _permissionsCreate(
              this.form,
              res => {
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          }
        }
      });
    },
    domInit() {
      _permissionsAll(
        res => {
          this.permissions = [];
          const permission = { id: 0, label: "顶级类目", children: [] };
          permission.children = res;
          this.permissions.push(permission);
        },
        err => {}
      );
    },
    dataInit() {
      this.form = {
        name: "",
        pid: 0,
        alias: ""
      };
    },
    closeDialog() {}
  }
};
</script>

<style lang="scss">
</style>
